package org.he.org.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.he.basic.annotation.HePermission;
import org.he.basic.util.AjaxResult;
import org.he.basic.util.PageInfo;
import org.he.org.domain.Department;
import org.he.org.domain.Employee;
import org.he.org.query.DepartmentQuery;
import org.he.org.service.IEmployeeService;
import org.he.org.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.nio.cs.ext.SJIS;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.*;

/**
 * @author He
 * @date 2023/11/30  10:45
 */
@RestController
@RequestMapping("/employee")
@Api(value = "员工管理Controller",description = "员工的增删查改接口")
@HePermission(name = "员工管理", desc = "管理员工的接口")
@Slf4j
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @PutMapping()
    @ApiOperation(value = "增加或修改员工")
    @HePermission(name = "员工增加或修改", desc = "员工增加或修改")
    public AjaxResult save(@RequestBody Employee employee) {
        try {
            if (Objects.isNull(employee.getId())) {
                employeeService.insert(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.success(employee,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败");
        }
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除员工")
    @HePermission(name = "删除员工", desc = "删除员工")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除失败");
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询指定员工")
    @HePermission(name = "查询指定员工", desc = "查询指定员工")
    public AjaxResult queryById(@PathVariable("id") Long id){
        try {
            Employee employee = employeeService.queryById(id);
            return AjaxResult.success(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败");
        }
    }

    @GetMapping
    @ApiOperation(value = "查询所有员工")
    @HePermission(name = "查询所有员工", desc = "查询所有员工")
    public AjaxResult getAll() {
        try {
            List<Employee> list = employeeService.queryAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除接口") // 不管是put请求还是post请求,还有patch请求这里要写@RequestBody
    @HePermission(name = "批量删除员工", desc = "批量删除员工")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败");
        }
    }

    @PostMapping
    @ApiOperation(value = "分页接口") // 不管是put请求还是post请求,这里要写@RequestBody
    @HePermission(name = "员工分页查询", desc = "员工分页查询")
    public AjaxResult queryPage(@RequestBody DepartmentQuery departmentQuery){
        try {
            PageInfo<Employee> page = employeeService.queryPage(departmentQuery);
            return AjaxResult.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
//    @GetMapping("/tree")
//    @ApiOperation(value = "查询部门树")
//    @HePermission(name = "查询部门树", desc = "查询部门树")
//    public AjaxResult getTree(){
//        try {
//            List<Department> departments = employeeService.getTree();
//            return AjaxResult.success(departments);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return AjaxResult.error();
//        }
//    }

    @PostMapping("/exportExcel")
    @ApiOperation(value = "导出策略列表Excel")
    public AjaxResult export(@RequestBody DepartmentQuery query) {
        try {
            employeeService.export(query);
            return AjaxResult.success("导出成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("导出失败");
        }
    }

    @PostMapping("/importExcel")
    public AjaxResult upload(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        try {
            List<Employee> list = employeeService.upload(multipartFile);
            System.out.println("--------------------------------"+list.size()+"--------------------------------");
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("导入失败");
        }
    }

}

